package io.wdb.modules.generator.generator.controller.operation;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import io.wdb.common.annotation.SysLog;
import io.wdb.common.utils.PageUtils;
import io.wdb.common.utils.Query;
import io.wdb.common.utils.R;
import io.wdb.modules.generator.generator.controller.api.BaseController;
import io.wdb.modules.generator.generator.entity.BookEntityEntity;
import io.wdb.modules.generator.generator.entity.customer.BookBorrowStatistics;
import io.wdb.modules.generator.generator.entity.customer.BookEntityCustomer;
import io.wdb.modules.generator.generator.entity.customer.OrderTransEntityCustomer;
import io.wdb.modules.generator.generator.entity.web.BaseResponse;
import io.wdb.modules.generator.generator.entity.web.WebResponse;
import io.wdb.modules.generator.generator.service.AccountCashService;
import io.wdb.modules.generator.generator.service.operation.BookBorrowOperationService;

@RestController
@RequestMapping("operation/book")
public class BookBorrowOperationController extends BaseController{
	private Logger logger = LoggerFactory.getLogger(BookBorrowOperationController.class);
	@Autowired
	private BookBorrowOperationService bookBorrowOperationService;
	
	@Autowired
	private AccountCashService accountCashService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("operation:book:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<OrderTransEntityCustomer> orderTransEntityCustomers = bookBorrowOperationService.queryList(query);
		
		PageInfo<OrderTransEntityCustomer> page = new PageInfo<OrderTransEntityCustomer>(orderTransEntityCustomers);
		int total = (int) page.getTotal();
		
		PageUtils pageUtil = new PageUtils(orderTransEntityCustomers, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	/**
	 * 
	 * @Method:update
	 * @Description:编辑状态，书籍出借，未出借状态不再进行修改
	 * @Time 2017年12月18日 下午4:25:04
	 * @author: Tom
	 * @return
	 */
	@Deprecated
	@RequestMapping("/update")
	public BaseResponse updateBorrowStatus(@RequestBody BookEntityCustomer bookEntityCustomer){
		BaseResponse baseResponse = success();
		try {
			WebResponse webResponse = bookBorrowOperationService.updateBorrowStatus(bookEntityCustomer);
			baseResponse.setDetail(webResponse);
		} catch (Exception e) {
			baseResponse=failed(e);
			logger.error(e.getMessage(),e);
		}
		return baseResponse;
	}
	@SysLog("更改书籍维护以及销售状态")
	@RequestMapping("/updateBookStatus")
	@RequiresPermissions("operation:book:update")
	public BaseResponse updateBookStatus(@RequestBody BookEntityEntity bookEntityEntity){
		BaseResponse baseResponse = success();
		try {
			WebResponse webResponse = bookBorrowOperationService.updateBookStatus(bookEntityEntity);
			baseResponse.setDetail(webResponse);
		} catch (Exception e) {
			baseResponse=failed(e);
			logger.error(e.getMessage(),e);
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:getBookBorrowCount
	 * @Description:书籍借阅次数
	 * @Time 2018年1月8日 上午11:22:26
	 * @author: Tom
	 * @param params
	 * @return
	 */
	@RequestMapping("/getBookBorrowCount")
	@RequiresPermissions("operation:book:list")
	public R getBookBorrowCount(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<OrderTransEntityCustomer> orderTransEntityCustomers = bookBorrowOperationService.queryBorrowCount(query);
		
		PageInfo<OrderTransEntityCustomer> page = new PageInfo<OrderTransEntityCustomer>(orderTransEntityCustomers);
		int total = (int) page.getTotal();
		
		PageUtils pageUtil = new PageUtils(orderTransEntityCustomers, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	/**
	 * 
	 * @Method:bookBorrowStatistics
	 * @Description:书籍借阅统计
	 * @Time 2018年1月11日 下午5:24:53
	 * @author: Tom
	 * @return
	 */
	@RequestMapping("/bookBorrowStatistics")
	@RequiresPermissions("operation:book:list")
	public BaseResponse bookBorrowStatistics(@RequestBody BookEntityEntity bookEntityEntity){
		BaseResponse baseResponse = success();
		try {
			BookBorrowStatistics bookBorrowStatistics = bookBorrowOperationService.bookBorrowStatistics(bookEntityEntity.getRfidCode());
			baseResponse.setDetail(bookBorrowStatistics);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage(),e);
		}
		return baseResponse;
	}
}
